clear all 
set more off

* simpler heat map in Figure 5a, wage down-same-up.

* load
use "Transitions_3Qtr_UDSEarnings.dta", clear

* transitions you want in the table
local t0list "UW NW WWu"
local t1list "U N B Wd Ws Wu"

* count of transitions (x,y)
local rnn1 = 3
local rnn2 = 6

* rearrange in a particular way to make life easier
foreach t0 of local t0list {
	foreach t1 of local t1list {

		gen _hold = ytrate if trans == "`t0'`t1'"
		bys ccode year: egen T_`t0'`t1' = max(_hold)
		drop _hold

	}
}

* get rid of stuff you don't need.
duplicates drop ccode year, force
drop ytrate


* create the heat map
* everything below here is super specific and in a particular order.
* best not to change it unless you want to play with color scheme (line 146)

matrix heat2 = J(`rnn1',`rnn2',.)
matrix heat2p = J(`rnn1',`rnn2',.)
matrix heat2ns = J(`rnn1',`rnn2',.)
matrix heat2se = J(`rnn1',`rnn2',.)


// The ordering of these needs to line up with the labels created below.
local rn = 1
foreach x of local t0list {

	local cn = 1
	foreach z of local t1list {
		reg T_`x'`z' lcgdp, cluster(ccode)
		matrix heat2[`rn',`cn'] = _b[lcgdp]
		matrix heat2p[`rn',`cn'] = (2 * ttail(e(df_r), abs(_b[lcgdp]/_se[lcgdp])))
		matrix heat2se[`rn',`cn'] = round(_se[lcgdp],0.001)

		local cn = `cn'+1
	}

	local rn = `rn' + 1
}



// Set heat1ns = . if not significant
forvalues i = 1/`rnn1' {
	forvalues j = 1/`rnn2' {

		if heat2p[`i',`j'] <= 0.10 {
			matrix heat2ns[`i',`j'] = heat2[`i',`j']
		}

	}
}


// Set significance stars
forvalues i = 1/`rnn1' {
	forvalues j = 1/`rnn2' {


		local se_`i'`j' = heat2se[`i',`j']
		local yse_`i'`j' = `i' + 0.15
		local xse_`i'`j' = `j' - 0

		// set the values for location of stars
		// y does not depend on anything. x value depends on stars
		local y_`i'`j' = `i' - 0.01



		// Compute the stars -- THIS DEPENDS ON SIZE OF THE MATRIX

		if heat2p[`i',`j'] > 0.10 {
			local x_`i'`j' = `j' + 0.21
			local star_`i'`j' ""
		}


		if heat2p[`i',`j'] > 0.05 & heat2p[`i',`j'] <= 0.10 {
			local star_`i'`j' "*" 

			// set x location if p in (0.01, 0.05]
			if heat2[`i',`j'] < 0 {
				local x_`i'`j' = `j' + 0.33
			}	
			else {
				local x_`i'`j' = `j' + 0.30
			}				
		}

		if heat2p[`i',`j'] > 0.01 & heat2p[`i',`j'] <= 0.05 {
			local star_`i'`j' "**" 

			// set x location if p in (0.01, 0.05]
			if heat2[`i',`j'] < 0 {
				local x_`i'`j' = `j' + 0.36
			}	
			else {
				local x_`i'`j' = `j' + 0.32
			}				
		}	

		if heat2p[`i',`j'] < 0.01 {
			local star_`i'`j' "***" 

			// set x location if p < 0.01
			if heat2[`i',`j'] < 0 {
				local x_`i'`j' = `j' + 0.40
			}	
			else {
				local x_`i'`j' = `j' + 0.37
			}			
		}	
	}
}

matrix rownames heat2 = U,W N,W W,Wu
matrix colnames heat2 = U N S Wd Ws Wu

matrix rownames heat2p = U,W N,W W,Wu
matrix colnames heat2p = U N S Wd Ws Wu

matrix rownames heat2ns = U,W N,W W,Wu
matrix colnames heat2ns = U N S Wd Ws Wu



colorpalette HSV heat, n(15) reverse nograph
*values(format(%9.2f)) colors(`r(p)',intensity(0.6))

* HCL pinkgreen 




#delimit ;
heatplot heat2, 
values(format(%9.3f) mlabsize(medsmall)) colors(HSV bluered2,intensity(1))
cuts(-0.15(0.02)0.15)
graphregion(color(white)) p(lcolor(black%50) lwidth(vthin))
xtitle("To:") ytitle("From:") 
xscale(alt lstyle(none)) xlabel(,tlength(0))
yscale(lstyle(none)) ylabel(,tlength(0) nogrid)
legend(off)
name(heat_2)

text(`y_11' `x_11' "`star_11'", size(small))
text(`y_12' `x_12' "`star_12'", size(small))
text(`y_13' `x_13' "`star_13'", size(small))
text(`y_14' `x_14' "`star_14'", size(small))
text(`y_15' `x_15' "`star_15'", size(small))
text(`y_16' `x_16' "`star_16'", size(small))

text(`y_21' `x_21' "`star_21'", size(small))
text(`y_22' `x_22' "`star_22'", size(small))
text(`y_23' `x_23' "`star_23'", size(small))
text(`y_21' `x_24' "`star_24'", size(small))
text(`y_22' `x_25' "`star_25'", size(small))
text(`y_23' `x_26' "`star_26'", size(small))	

text(`y_31' `x_31' "`star_31'", size(small))
text(`y_32' `x_32' "`star_32'", size(small))
text(`y_33' `x_33' "`star_33'", size(small))
text(`y_31' `x_34' "`star_34'", size(small))
text(`y_32' `x_35' "`star_35'", size(small))
text(`y_33' `x_36' "`star_36'", size(small))		

text(`yse_11' `xse_11' "(0`se_11')", size(medsmall))
text(`yse_12' `xse_12' "(0`se_12')", size(medsmall))
text(`yse_13' `xse_13' "(0`se_13')", size(medsmall))
text(`yse_14' `xse_14' "(0`se_14')", size(medsmall))
text(`yse_15' `xse_15' "(0`se_15')", size(medsmall))
text(`yse_16' `xse_16' "(0`se_16')", size(medsmall))

text(`yse_21' `xse_21' "(0`se_21')", size(medsmall))
text(`yse_22' `xse_22' "(0`se_22')", size(medsmall))
text(`yse_23' `xse_23' "(0`se_23')", size(medsmall))
text(`yse_24' `xse_24' "(0`se_24')", size(medsmall))
text(`yse_25' `xse_25' "(0`se_25')", size(medsmall))
text(`yse_26' `xse_26' "(0`se_26')", size(medsmall))

text(`yse_31' `xse_31' "(0`se_31')", size(medsmall))
text(`yse_32' `xse_32' "(0`se_32')", size(medsmall))
text(`yse_33' `xse_33' "(0`se_33')", size(medsmall))
text(`yse_34' `xse_34' "(0`se_34')", size(medsmall))
text(`yse_35' `xse_35' "(0`se_35')", size(medsmall))
text(`yse_36' `xse_36' "(0`se_36')", size(medsmall))
;
#delimit cr

graph export "Figures/Figure5A.tif", replace
graph export "Figures/pdf_figures/Figure5A.pdf", replace



* ------------------- In Text Numbers: Down/Out Elasticity

foreach y in U N B Wd {
	replace T_UW`y' = 0 if missing(T_UW`y')
	replace T_NW`y' = 0 if missing(T_NW`y')
	replace T_WWu`y' = 0 if missing(T_WWu`y')

}	


gen T_UWout = T_UWU + T_UWN + T_UWB + T_UWWd
gen T_NWout = T_NWU + T_NWN + T_NWB + T_NWWd
gen T_WWuout = T_WWuU + T_WWuN + T_WWuB + T_WWuWd

* read from command window.
reg T_UWout lcgdp, cluster(ccode)
reg T_NWout lcgdp, cluster(ccode)
reg T_WWuout lcgdp, cluster(ccode)

